package com.jinqing.wuchuang.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jinqing.wuchuang.entity.AuthUser;
import com.jinqing.wuchuang.entity.Login;
import com.jinqing.wuchuang.entity.SettledLegalRepresentative;
import com.jinqing.wuchuang.entity.User;
import com.jinqing.wuchuang.mapper.AuthUserMapper;
import com.jinqing.wuchuang.mapper.LoginMapper;
import com.jinqing.wuchuang.mapper.SettledLegalRepresentativeMapper;
import com.jinqing.wuchuang.mapper.UserMapper;
import com.jinqing.wuchuang.service.AuthUserService;
import com.jinqing.wuchuang.service.SettledLegalRepresentativeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;

@Service
public class SettledLegalRepresentativeServiceImpl implements SettledLegalRepresentativeService {
    @Autowired
    private SettledLegalRepresentativeMapper settledLegalRepresentativeMapper;
    @Autowired
    private AuthUserService authService;
    @Autowired
    private AuthUserMapper authUserMapper;



    /**
     * 更新法人代表信息
     * @param settledLegalRepresentative
     */
    @Override
    public void updateRepresentativeInformation(SettledLegalRepresentative settledLegalRepresentative) {
        /**
         * 先判断存不存在，存在更新不存在就插入
         * 获取登录用户的uid，查看这个uid有没有法人信息，没有就插入
         * 有就更新
         */
        Long uid = authService.getUidByToken();
        SettledLegalRepresentative Representative = getSettledLegalRepresentative();
        if(Representative==null){
            //说明第一次完善信息

            settledLegalRepresentative.setUid(uid);
            settledLegalRepresentativeMapper.insert(settledLegalRepresentative);
        }
        else {
            //说明之前完善过，修改
            UpdateWrapper<SettledLegalRepresentative> updateWrapper = new UpdateWrapper<SettledLegalRepresentative>();
            updateWrapper.eq("uid",uid);
            settledLegalRepresentativeMapper.update(settledLegalRepresentative,updateWrapper);
        }

    }

    /**
     * 获取法人信息
     * @return
     */
    @Override
    public SettledLegalRepresentative getSettledLegalRepresentative() {
        String userName =(String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long uid = authService.getUidByuserName(userName);
        return settledLegalRepresentativeMapper.getSettledLegalRepresentativeByUid(uid);
    }

    /**
     * 查看当前登录用户是否完善个人信息
     * @return
     */
    @Override
    public boolean isImproveInformation() {
        //获取法人信息，判断所有变量是否为空就好
        SettledLegalRepresentative settledLegalRepresentative = getSettledLegalRepresentative();
        try {
            for (Field field : settledLegalRepresentative.getClass().getDeclaredFields()) {
                //把私有属性公有化
                field.setAccessible(true);
                Object object = field.get(settledLegalRepresentative);
                if (object==null) {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }



}
